package com.qianteng.testcliper;


import android.app.Activity;
import android.text.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;

import android.util.Log;
import android.widget.Toast;

/*
 * Receives broadcast commands and controls clipboard accordingly.
 * The broadcast receiver is active only as long as the application, or its service is active.
 */
public class ClipperReceiver extends BroadcastReceiver {
    private static String TAG = "ClipboardReceiver";

    public static String ACTION_GET = "clipper.get";
    public static String ACTION_GET_SHORT = "get";
    public static String ACTION_SET = "clipper.set";
    public static String ACTION_SET_SHORT = "set";
    public static String EXTRA_TEXT = "text";
    public static String ACTION_INFO = "clipper.info";
    public static String ACTION_INFO_SHORT = "info";

    public static boolean isActionGet(final String action) {
        return ACTION_GET.equals(action) || ACTION_GET_SHORT.equals(action);
    }

    public static boolean isActionSet(final String action) {
        return ACTION_SET.equals(action) || ACTION_SET_SHORT.equals(action);
    }
    public static boolean isActionInfo(final String action) {
        return ACTION_INFO.equals(action) || ACTION_INFO_SHORT.equals(action);
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        ClipboardManager cb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (isActionSet(intent.getAction())) {
            Log.d(TAG, "Setting text into clipboard");
            String text = intent.getStringExtra(EXTRA_TEXT);
            if (text != null) {
                String textnew = text.replace("[nr]","\n");
                textnew = textnew.replace("[s]"," ");
                cb.setText(textnew);
                setResultCode(Activity.RESULT_OK);
                setResultData("Text is copied into clipboard.");
            } else {
                setResultCode(Activity.RESULT_CANCELED);
                setResultData("No text is provided. Use -e text \"text to be pasted\"");
            }
        } else if (isActionGet(intent.getAction())) {
            Log.d(TAG, "Getting text from clipboard");
            CharSequence clip = cb.getText();

            String text = intent.getStringExtra(EXTRA_TEXT);
            if (text != null) {
                String textnew = text.replace("[nr]","\n");
                cb.setText(textnew);
                setResultCode(Activity.RESULT_OK);
                setResultData("Text is copied into clipboard.");
            }
            if (clip != null) {
                Log.d(TAG, String.format("Clipboard text: %s", clip));
                setResultCode(Activity.RESULT_OK);
                setResultData(clip.toString());
            } else {
                setResultCode(Activity.RESULT_CANCELED);
                setResultData("");
            }
        }else if (isActionInfo(intent.getAction()))
        {

            Log.d(TAG, "info text into infoing");

            String text = intent.getStringExtra(EXTRA_TEXT);
            if (text != null) {
                String textnew = text.replace("[nr]","\n");
//                cb.setText(textnew);
                setResultCode(Activity.RESULT_OK);
                Toast toast = Toast.makeText(context, textnew, Toast.LENGTH_SHORT);
                toast.show();
            }



        }
    }
}